<?php
/**
 * Created by PhpStorm.
 * User: zhangchangshun
 * Date: 17-3-20
 * Time: 下午3:45
 */
class topic extends API_Controller
{
    public function __construct()
    {
        parent::__construct();
        if(ENVIRONMENT=='production'){
            $this->_auto();
        }
        ini_set('date.timezone','Asia/Shanghai'); // 'Asia/Shanghai'
        $this->load->model('topic_model', 'mtopic');
        $this->load->model('article_model', 'marticle');
        $this->load->model('circle_model','mcircle');
        $this->load->model('account_model','maccount');
        $this->load->model('upload_model','mupload');
        $this->load->model('comments_model','mcomments');
        $this->load->model('concerns_model', 'mconcerns');

    }

    /**
     * 话题详情
     */
    public function topic_detail(){
        $article_id = $this->input->post('id');
        $article_info = $this->mtopic->get_one(array('id'=>$article_id,'is_deleted'=>0));
        if(empty($article_info)){
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '话题不存在';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            $info['circle_photo'] = array();
            if($article_info['photo']!=''){
                $c_photos = explode(',',$article_info['photo']);
                foreach($c_photos as $c_photo){
                    $img = $this->mupload->get_one(array('id'=>$c_photo));
                    if(!empty($img)){
                        $info['circle_photo'][] = UPLOAD_URL.$img['url'];
                    }
                }
            }
            //是否收藏和关注
            $my_concerns = $this->mconcerns->get_one(array('uid'=>$this->userinfo['id'],'tid'=>$article_id,'is_deleted'=>1));
            if(!empty($my_concerns)){
                $info['is_concern']=0;
            }else{
                $info['is_concern']=1;
            }
            $info['content'] = $article_info['content'];
            $info['title'] = $article_info['title'];
            $info['id'] = $article_id;
            $info['cre_time'] = date('m-d H:i',$article_info['timeline']);
            $info['comments'] = $article_info['comments'];
            $info['collects'] = $article_info['collects'];
            $info['concern'] = $article_info['concern'];
            $info['click'] = $article_info['click'];
            //话题下的新闻列表
            $child = $this->marticle->get_all(array('cid'=>2,'ccid'=>$article_id),'id,title',array('timeline'=>'desc'));
            foreach($child as $kkc=>$vvc){
                $string = $vvc['title'];
                if(strlen($string) >= 80){
                    $child[$kkc]['title'] = mb_substr($string,0,25,'utf-8').'...';
                }else{
                    $child[$kkc]['title'] = $string;
                }
            }
            $info['news'] = $child;
            //浏览量+1
            $this->mtopic->set($article_id,array('click','click + 1'));
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= $info;
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 评论详情列表展示(下拉加载)
     */
    public function circle_comments(){
        $id = $this->input->post('id');
        $type = $this->input->post('type');
        $start = $this->input->post('page');
        $limit = 5;
        $start = $limit*($start-1);
        $comments = $this->mcomments->get_list($limit,$start,array('created'=>'desc'),array('rid'=>$id,'type'=>$type,'pid'=>0),"*");
        //获取当前用户信息
        $user_info = $this->userinfo;
        $uid = $user_info['id'];
        if(empty($comments)){
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            foreach($comments as $kk=>$vv){
                if($vv['uid']==$uid){
                    $comments[$kk]['is_can']=0;
                }else{
                    $comments[$kk]['is_can']=1;
                }
                $user_info = $this->maccount->get_one(array('id'=>$vv['uid']));
                $comments[$kk]['uname']=$user_info['uname'];
                $photo = $this->mupload->get_one(array('id'=>$user_info['photo']));
                if(!empty($photo)){
                    $comments[$kk]['headimg'] = UPLOAD_URL.$photo['url'];
                }else{
                    $comments[$kk]['headimg'] = '';
                }
                $comments[$kk]['cre_time'] = date('m-d H:i',$vv['created']);
                $child = $this->mcomments->get_all(array('rid'=>$id,'type'=>$type,'pid'=>$vv['id']),'*',array('created'=>'asc'));
                foreach($child as $key=>$val){
                    $user_info = $this->maccount->get_one(array('id'=>$val['uid']));
                    $user_info2 = $this->maccount->get_one(array('id'=>$val['chat_id']));
                    $child[$key]['uname']=$user_info['uname'];
                    if(!empty($user_info2)){
                        $child[$key]['cname']=$user_info2['uname'];
                    }else{
                        $child[$key]['cname']='';
                    }
                    if($val['uid']==$uid){
                        $child[$key]['is_can']=0;
                    }else{
                        $child[$key]['is_can']=1;
                    }
                }
                $comments[$kk]['child']=$child;
                $comments[$kk]['childnums']=count($child);
            }
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= $comments;
        }
        $this->_send_json($this->vdata);
    }

    /**
     * 新闻详情
     */
    public function news_detail(){
        $article_id = $this->input->post('id');
        $article_info = $this->marticle->get_one(array('id'=>$article_id));
        if(empty($article_info)){
            $this->vdata['returnCode']   = '250';
            $this->vdata['returnInfo'] = '新闻不存在';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= array();
        }else{
            $info['circle_photo'] = array();
            if($article_info['photo']!=''){
                $c_photos = explode(',',$article_info['photo']);
                foreach($c_photos as $c_photo){
                    $img = $this->mupload->get_one(array('id'=>$c_photo));
                    if(!empty($img)){
                        $info['news_photo'][] = UPLOAD_URL.$img['url'];
                    }
                }
            }
            $info['content'] = $article_info['content'];
            $info['title'] = $article_info['title'];
            $info['source'] = $article_info['source'];
            $info['source_url'] = $article_info['source_url'];
            $info['id'] = $article_id;
            $info['cre_time'] = date('m-d H:i',$article_info['timeline']);
            $this->vdata['returnCode']   = '200';
            $this->vdata['returnInfo'] = '获取成功';
            $this->vdata['secure']     = JSON_SECURE;
            $this->vdata['content']= $info;
        }
        $this->_send_json($this->vdata);
    }
}